<?php

namespace App\Http\Controllers\API;

use App\Model\Document;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class DocumentController extends Controller
{
    /**
     * @abstract get all document data
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        $arrRequestParams = $request->all();
        if(array_key_exists('documentname',$arrRequestParams) && $arrRequestParams['documentname'] != ''){
            $arrDocument = DB::table('documents')->where('documentname', 'like', '%'.$arrRequestParams['documentname'].'%')->get();
        }else{
            $arrDocument = DB::table('documents')->get();
        }
        return response()->json(['code' => 0, 'message' => 'success', 'data' => $arrDocument],200);
    }

    /**
     * @abstract add document
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request){
        $arrInsertData = $request->all();
        $arrFileInfo = explode('.',$arrInsertData['path']);
        $objDocument = new Document();
        $objDocument->documentname = $arrInsertData['documentname'];
        $objDocument->filetype = $arrFileInfo[1];
        $objDocument->showtype = $arrInsertData['showtype'];
        $objDocument->path = $arrInsertData['path'];
        $objDocument->status = 0;
        $objUser = $request->session()->get('userinfo');
        if(is_object($objUser)){
            $objDocument->creator = $objUser->id;
        }else{
            $objDocument->creator = 0;
        }
        $objDocument->save();
        return response()->json(['code' => 0, 'message' => 'success'],200);
    }

    /**
     * @abstract update document
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request,$id){
        $arrInsertData = $request->all();
        $arrFileInfo = explode('.',$arrInsertData['path']);
        $objDocument = Document::findOrFail($id);
        $objDocument->documentname = $arrInsertData['documentname'];
        $objDocument->filetype = $arrFileInfo[1];
        $objDocument->showtype = $arrInsertData['showtype'];
        $objDocument->path = $arrInsertData['path'];
        $objDocument->status = 0;
        $objUser = $request->session()->get('userinfo');
        if(is_object($objUser)){
            $objDocument->creator = $objUser->id;
        }else{
            $objDocument->creator = 0;
        }
        $objDocument->save();
        return response()->json(['code' => 0, 'message' => 'success'],200);
    }


    /**
     * @abstract get document detail data by id
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id){
        $objDocument = Document::find($id);
        return response()->json(['code' => 0, 'message' => 'success', 'data' => $objDocument],200);
    }

    /**
     * @abstract get data by showtype
     * @param $showtype
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDocumentByShowType($showtype){
        $arrDocuments = DB::select('select * from documents where showtype = ?',[$showtype]);
        return response()->json(['code' => 0, 'message' => 'success', 'data' => $arrDocuments],200);
    }

    /**
     * @abstract update document status
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateStatus($id){
        DB::update('update documents set status = ? where id = ?',[1,$id]);
        return response()->json(['code' => 0, 'message' => 'update success'],200);
    }


    /**
     * @abstrtact delete document
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id){
        DB::delete('delete from documents where id = ?',[$id]);
        return response()->json(['code' => 0, 'message' => 'delete success'],200);
    }
}
